<script type="text/javascript">
	function submitClicked(string) {
		document.new_scholarship.action = "validate_finish_scholarship.jsp?scholarship_id=" +string;
		javascript: new_scholarship.submit();
	}
	function saveClicked() {
		document.getElementsByName("finished").value = false;
		var data = document.createElement("input");
		data.setAttribute('type', "hidden");
		data.setAttribute('name', "scholarship_id");
		data.setAttribute('value', scholarship_id);
		new_scholarship.appendChild(data);
		javascript: new_scholarship.submit();
	}
</script>
<h1 class="center_text">Finish A New Scholarship</h1>
<hr />
<form name="new_scholarship" action="validate_finish_scholarship.jsp" method="post" enctype="multipart/form-data">
	<table class="right_text center">
		<%
			// Provide default values for variables.
			if (session.getAttribute("errors") == null) {
				session.setAttribute("errors", "");
			}
			if (session.getAttribute("name") == null) {
				session.setAttribute("name", "");
			}
			if (session.getAttribute("sponsors") == null) {
				session.setAttribute("sponsors", "");
			}
			if (session.getAttribute("reward") == null) {
				session.setAttribute("reward", "");
			}
			if (session.getAttribute("num_recipients") == null) {
				session.setAttribute("num_recipients", "");
			}
			if (session.getAttribute("major") == null) {
				session.setAttribute("major", "");
			}
			if (session.getAttribute("min_gpa") == null) {
				session.setAttribute("min_gpa", "");
			}
			if (session.getAttribute("min_standing") == null) {
				session.setAttribute("min_standing", "");
			}
			if (session.getAttribute("max_standing") == null) {
				session.setAttribute("max_standing", "");
			}
			if (session.getAttribute("deadline") == null) {
				session.setAttribute("deadline", "");
			}
			if (session.getAttribute("description") == null) {
				session.setAttribute("description", "");
			}

			// Retrieve session variables.
			String type = (String) session.getAttribute("type");
			String user = (String) session.getAttribute("user");
			String errors = (String) session.getAttribute("errors");
			String name = (String) session.getAttribute("name");
			String sponsors = (String) session.getAttribute("sponsors");
			String reward = (String) session.getAttribute("reward");
			String num_recipients = (String) session.getAttribute("num_recipients");
			String major = (String) session.getAttribute("major");
			String min_gpa = (String) session.getAttribute("min_gpa");
			String min_standing = (String) session.getAttribute("min_standing");
			String max_standing = (String) session.getAttribute("max_standing");
			String deadline = (String) session.getAttribute("deadline");
			String description = (String) session.getAttribute("description");
			String[] values = { name, sponsors, reward, num_recipients, major, min_gpa, min_standing, max_standing, deadline, description };
		%>
		<tr>
			<td></td>
			<td><div style="color: red; font-weight: bold; text-align: left;"><%=errors%></div></td>
		</tr>
		<%
			// Clear variables after reading them.
			session.setAttribute("errors", null);
			session.setAttribute("name", null);
			session.setAttribute("sponsors", null);
			session.setAttribute("reward", null);
			session.setAttribute("num_recipients", null);
			session.setAttribute("major", null);
			session.setAttribute("min_gpa", null);
			session.setAttribute("min_standing", null);
			session.setAttribute("max_standing", null);
			session.setAttribute("deadline", null);
			session.setAttribute("description", null);

			// Getting information and updating the database.
			java.sql.Connection conn = null;
			try {
				String myJDBCDriver = (String) session.getAttribute("myJDBCDriver");
				String myURL = (String) session.getAttribute("myURL");
				String myUserID = (String) session.getAttribute("myUserID");
				String myPassword = (String) session.getAttribute("myPassword");

				// Connect to the database.
				Class.forName(myJDBCDriver).newInstance();
				java.util.Properties sysprops = System.getProperties();
				sysprops.put("user", myUserID);
				sysprops.put("password", myPassword);
				conn = java.sql.DriverManager.getConnection(myURL, sysprops);

				java.sql.Statement statement = conn.createStatement();
				java.sql.ResultSet rs = statement
						.executeQuery("SELECT * FROM scholarships WHERE id='" + request.getParameter("scholarship_id") + "'");
				java.sql.ResultSetMetaData columns = rs.getMetaData();
				rs.next();
				for (int i = 2; i <= columns.getColumnCount() - 2; i++) {
					// The first index is the scholarship id. The last index is the submitter's username. The second to last index indicates whether
					// or not this scholarship is ready to be applied for.
					out.println("<tr><th>" + columns.getColumnName(i) + "</th>");
					if (columns.getColumnName(i).equalsIgnoreCase("description")) {
						out.println("<td><textarea cols=\"49\" rows=\"6\" name =\"" + columns.getColumnName(i) + "\">" + rs.getString("description")
								+ "</textarea></td></tr>");
					} else {
						out.println("<td><input type=\"text\" size=\"64\" value=\"" + rs.getString(columns.getColumnName(i)) + "\" name =\""
								+ columns.getColumnName(i) + "\" /></td></tr>");
					}
				}
				out.println("<tr><th>Additional Information</th><td><input type=\"file\" name=\"document\" style=\"float:left;\"/></td></tr>");
			} catch (Exception e) {
				e.printStackTrace();
				out.print(e.toString());
			} finally {
				try {
					conn.close();
				} catch (Exception ee) {
				}
			}
		%>
		<tr>
			<td><input type="hidden" name="finished" value="true" /> <a href="Javascript:void();" class="right red_button" onclick="saveClicked()">Save</a></td>
			<td><a href="Javascript:void();" class="right red_button" onclick="submitClicked(<%=request.getParameter("scholarship_id")%>)">Submit</a></td>
		</tr>
	</table>
</form>